package com.wellink.gridviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private GridView gridView;

    private int[] icons = {
            R.drawable.icon1,R.drawable.icon2,R.drawable.icon3,
            R.drawable.icon4,R.drawable.icon5,R.drawable.icon6,R.drawable.icon7
    };

    private String[] names = {
            "魔法棒", "点赞社群", "购物街区","蚂蚁社区","鑫鱻地图",
            "鑫鱻消息", "房品汇"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridView = findViewById(R.id.gridview);

        List<Map<String,Object>> list = new ArrayList<>();

        for (int i=0;i<icons.length;i++){
            Map<String,Object> map = new HashMap<>();
            map.put("icon",icons[i]);
            map.put("name",names[i]);
            list.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.item_layout,
                new String[]{"icon","name"},new int[]{R.id.icon,R.id.text});

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,names[position],Toast.LENGTH_LONG).show();
            }
        });

    }
}
